//
// DepRepoUploader
//
// Copyright (C) jextra.net.
//
//  This file is part of the net.jextra.dep Library.
//
//  The net.jextra.dep Library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2.1 of the License, or (at your option) any later version.
//
//  The net.jextra.dep Library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with the net.jextra.dep Library; if not, write to the Free
//  Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
//  02111-1307 USA.
//

package net.jextra.dep.repo;

import java.io.*;
import net.jextra.dep.*;

/**
 * User helper to upload files to a {@link DepRepo}.
 */
public class DepRepoUploader
{
    // ============================================================
    // Fields
    // ============================================================

    private DepRepo repo;

    // ============================================================
    // Constructors
    // ============================================================

    public DepRepoUploader( DepRepo repo )
    {
        this.repo = repo;
    }

    // ============================================================
    // Methods
    // ============================================================

    // ----------
    // public
    // ----------

    public void setDepRepo( DepRepo repo )
    {
        this.repo = repo;
    }

    public void uploadDir( File fromDir, String componentName, String versionNumber, String repoPath )
    {
        assert fromDir != null : "fromDir can not be null.";
        assert fromDir.exists() : "fromDir does not exist. " + fromDir.getPath();
        assert fromDir.isDirectory() : "fromDir must be a directory.";
        assert componentName != null;
        assert versionNumber != null;

        if ( repoPath == null )
        {
            repoPath = "";
        }

        try
        {
            System.out.printf( "Uploading directory [%s] to repository [%s/%s/%s].\n", fromDir.getCanonicalPath(),
                componentName, versionNumber, repoPath );
            for ( File file : fromDir.listFiles() )
            {
                if ( file.isDirectory() )
                {
                    uploadDir( file, componentName, versionNumber, repoPath + "/" + file.getName() );
                }
                else
                {
                    uploadFile( file, componentName, versionNumber, repoPath + "/" + file.getName() );
                }
            }
        }
        catch ( Exception ex )
        {
            throw new DepException( ex );
        }
    }

    public void uploadFile( File file, String componentName, String versionNumber, String repoPath )
    {
        assert file != null : "file can not be null.";
        assert file.exists() : "file does not exist. " + file.getPath();
        assert file.isFile() : "file must be a file.";
        assert componentName != null;
        assert versionNumber != null;

        try
        {
            System.out.printf( "Uploading file [%s] to repository file [%s].\n", file.getCanonicalPath(), repoPath );

            repo.uploadFile( componentName, versionNumber, repoPath, file );
        }
        catch ( Exception ex )
        {
            throw new DepException( ex );
        }
    }
}
